package chapter18.chapter18_1;//: io/DirList2.java
// Uses anonymous inner classes.
// {Args: "D.*\.java"}

import java.util.regex.*;
import java.io.*;
import java.util.*;

public class DirList2 {
    public static FilenameFilter filter(final String regex) {
        // Creation of anonymous inner class:
        return new FilenameFilter() {
            private Pattern pattern = Pattern.compile(regex);

            public boolean accept(File dir, String name) {
                return pattern.matcher(name).matches();
            }
        }; // End of anonymous inner class
    }

    public static void main(String[] args) {
        File path = new File(".\\src\\chapter18\\chapter18_1");
        String reg = "D.*\\.java";
        String[] list1;
        String[] list2;
        list1 = path.list();
        Arrays.sort(list1, String.CASE_INSENSITIVE_ORDER);
        for (String dirItem : list1)
            System.out.println(dirItem);
        //注意，因为String本身就是final的，所以filter才能接受reg作为参数
        System.out.println("\nuse DirFilter:");
        list2 = path.list(filter(reg));
        Arrays.sort(list2, String.CASE_INSENSITIVE_ORDER);
        for (String dirItem : list2)
            System.out.println(dirItem);
    }
} /* Output:
DirectoryDemo.java
DirList.java
DirList2.java
DirList3.java
MakeDirectories.java

use DirFilter:
DirectoryDemo.java
DirList.java
DirList2.java
DirList3.java
*///:~
